package com.ejlchina.tool.singleton

import com.huyuhui.fastble.data.BleDevice
import com.ejlchina.tool.bean.BleDeviceBean
import com.ejlchina.tool.bean.DeviceVersionsBean
import com.ejlchina.tool.constant.DEVICE_MODE_NO_SELECT
import com.ejlchina.tool.network.BucketIndexInfo

object GlobalParams {
    var statusBarHeight = 0

    val connectDeviceVersionList = hashSetOf<DeviceVersionsBean>()

    val deviceMode = DEVICE_MODE_NO_SELECT

    //这个列表是固件升级列表
    val firmwareUpgradeList = mutableListOf<BleDeviceBean>()

    val bucketIndexList = mutableListOf<BucketIndexInfo>()

    var isStartDebugMode: Boolean = false //是否开启调试模式

    fun isAllDeviceUpgradeComplete(): Boolean {
        return firmwareUpgradeList.all { it.upgradeResult >= 100F || it.upgradeResult < 0F }
    }

    fun isDeviceInFirmwareUpgrade(bleDeviceBean: BleDeviceBean): Boolean{
        return firmwareUpgradeList.find { it.bleDevice.mac == bleDeviceBean.bleDevice.mac && it.isInFirmwareUpgrade() } != null
    }

    fun isDeviceInFirmwareUpgrade(deviceMac: String): Boolean{
        return firmwareUpgradeList.find { it.bleDevice.mac == deviceMac && it.isInFirmwareUpgrade() } != null
    }

    fun getDeviceFromMac(mac: String): BleDeviceBean?{
        return firmwareUpgradeList.find { it.bleDevice.mac == mac }
    }

    fun addConnectDeviceVersionBean(mac: String,firmwareVersion: String,hardVersion: String){
        connectDeviceVersionList.add(DeviceVersionsBean(mac,firmwareVersion, hardVersion))
    }

    fun removeConnectDeviceVersionBean(mac: String){
        connectDeviceVersionList.removeIf { it.mac == mac }
    }

    fun getRealUpgradeVersions() :MutableSet<String>{
        val upgradeVersionSet = mutableSetOf<String>()
        for (item in firmwareUpgradeList){
            upgradeVersionSet.add(item.getRealUpgradeVersion())
        }
        return upgradeVersionSet
    }

    fun isDeviceInUpgrade(mac: String) = firmwareUpgradeList.indexOfFirst { it.bleDevice.mac == mac } != -1

    fun clearUpgradeListOrFinish(){
        firmwareUpgradeList.removeAll {
            it.upgradeResult >= 100F
        }
    }

    fun setDeviceListIndexFixInfo(deviceList: MutableList<BleDeviceBean>){
        deviceList.forEach { bleDeviceBean ->
            val bucketIndexBean =  bucketIndexList.find { bucketIndexInfo -> bucketIndexInfo.mac == bleDeviceBean.bleDevice.mac }
            bucketIndexBean?.let {
                bleDeviceBean.deviceNo = it.deviceNo
                bleDeviceBean.bucketNo = it.bucketNo
            }
        }
    }

    fun getBucketIndexFormBleDevice(bleDevice: BleDevice): String{
        var bucketIndex = ""
        bucketIndexList.forEach {
            if (it.mac == bleDevice.mac){
                bucketIndex = it.getBucketNoString()
                return bucketIndex
            }
        }
        return bucketIndex
    }
}